/** 
* Projet AP5 - Agenda PDA
* ContactEngine.java
*/

package pda.datas;

import java.io.Serializable;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.ArrayList;

/** Classe qui permet la gestion des contacts, par la sauvegarde,
* le chargement, l'ajout, la suppression, la modification,
* la consultation, et l'existence du contact
*/

public class ContactEngine implements Serializable{

//##########################################################################################################
//										Attributs
//##########################################################################################################
	/** Hashtable de String et Contact */ 
	private Hashtable<String, Contact> tabContacts ;

	/** ID de numero de  version non modifiable */
	private static final long serialVersionUID = 42 ;
	

//##########################################################################################################
//										Constructeur
//##########################################################################################################

	/**    
	* Constructeur de la classe
	*/ 
	public ContactEngine(){
		tabContacts = new Hashtable<String, Contact>();
	}


//##########################################################################################################
//										Methodes
//##########################################################################################################
	
	// @SuppressWarnings("unchecked")

	/**
	*this method load the specific list of contact in the array 
	*
	*/
	public void load(){
        
	    try{
		FileInputStream in = new FileInputStream ("data/sauvegarde/contact.out");
		ObjectInputStream flux = new ObjectInputStream (in);
		ContactEngine tmp = (ContactEngine)flux.readObject();
		this.tabContacts = tmp.tabContacts;
	    }
	    catch (IOException e){
		System.out.println (e.getMessage());
	    }
	    catch (ClassNotFoundException e){
		System.out.println(e.getMessage());
	    }
	}

	/**
	*this method save the specific list of contact from the array to the file 
	*
	*/
	public void save(){
        
	    try{
		FileOutputStream out = new FileOutputStream ("data/sauvegarde/contact.out");
		ObjectOutputStream flux = new ObjectOutputStream (out);
		flux.writeObject (this);
	    }
	    catch (FileNotFoundException e){
		System.out.println (e.getMessage ());
	    }
	    catch (IOException e){
		System.out.println (e.getMessage ());
	    }
	}

	/**
	*this can add a new contact to the Annuaire
	*
	*@param cle the key which identify the contact and which be displayed 
	*@param uneContact the Contact where there are all information about the contact
	*/
	public void ajouter(String cle,  Contact uneContact){
	    tabContacts.put(cle, uneContact);
	}

	/**
	*this can delete a contact from the Annuaire
	*
	*@param cle the key which identify the contact to delete
	*/
	public void supprimer(String cle){
		    tabContacts.remove(cle);
	}

	/**
	*this can edit a contact from the Annuaire
	*
	*@param cle the key which identify the contact and which be displayed 
	*@param uneContact the Contact where there are all the new information about the contact
	*/
	public void modifier(String cle, Contact uneContact){
	    tabContacts.remove(cle);
	    tabContacts.put(cle, uneContact);
	}

	/**
	*this can be use to display contact's information
	*
	*@param nom the key which is searched
	*@return the Contact with the information
	*/
	public Contact consulter(String nom){
		return tabContacts.get(nom);
	}

	/**
	*this method return the number of contacts
	*
	*@return the number of the contact
	*/
	public int taille(){
	    return tabContacts.size();
	}

	/**
	*this can be use to search if the contact is already existing
	*
	*@param cle the key which is searched
	*@return true if contact already existing
	*/
	public boolean existe(String cle){
	    return tabContacts.containsKey(cle);
	}

	/**
	*use it to have the entire list of contact
	*
	*@return the list of contact in an Enumeration of String
	*/
	public Enumeration<String> getKey(){
	    return tabContacts.keys();
	}

}